﻿using Dapper;
using Entity;
using Microsoft.Data.SqlClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IRealize
{
    public class R : IR
    {
        public async Task<int> Add(string name, string sm, string zt, string sqlserver)
        {
            using (SqlConnection con = new SqlConnection(sqlserver))
            {
                string sql = $@"insert into Roles values('{name}','{sm}','{zt}')";
                return await con.ExecuteAsync(sql);
            }
        }

        public async Task<int> Delete(string id, string sqlserver)
        {
            using (SqlConnection con = new SqlConnection(sqlserver))
            {
                string sql = $@"delete from Roles where RolesID = '{id}'";
                return await con.ExecuteAsync(sql);
            }
        }

        public async Task<List<Roles>> select(string sqlserver)
        {
            Task<List<Roles>> task = Task<List<Roles>>.Run(async () =>
            {
                using (SqlConnection con = new SqlConnection(sqlserver))
                {

                    string sql = $@"select * from Roles";
                    return con.Query<Roles>(sql).ToList();
                }
            });
            return await task;
        }

        public async Task<Roles> selectById(string id, string sqlserver)
        {
            Task<Roles?> task = Task<Roles>.Run(async () =>
            {
                using (SqlConnection con = new SqlConnection(sqlserver))
                {

                    string sql = $@"select * from Roles where RolesID = {id}";
                    return con.Query<Roles>(sql).FirstOrDefault();
                }
            });
            return await task;
        }
    }
}
